(function() {
	var _idMap = {};
	var _sendMsg = function(data, callback) {
		_idMap[data.id] = {
			data : data,
			callback : callback
		};// 记录消息以供成功后回调
		parent.window.webSocket_send(data);
	}
	var _msgCallBack = function(respData) {
		if (respData.status >= 200 && respData.status < 400) {
			var obj = _idMap[respData.id];
			console.log('_idMap:' + respData.id + ':' + obj);
			if (obj != null) {
				obj.callback(obj.data, respData);
			}
		}
	}

	// 公共逻辑
	var _statusChange = function(isOnline) {
		if (isOnline) {
			$('#statusCon').hide();
			$('#lockScreenBtn').attr('disabled', false);
			$('#clearDataBtn').attr('disabled', false);
			$('#sendTextBtn').attr('disabled', false);
		} else {
			$('#statusCon').show();
			$('#lockScreenBtn').attr('disabled', true);
			$('#clearDataBtn').attr('disabled', true);
			$('#sendTextBtn').attr('disabled', true);
		}
	}

	// 自有逻辑
	var _showLockScreen = function(isLock) {
		if (isLock) {
			$('#lockScreenCon').hide();
			$('#unlockScreenCon').show();
			$('#lockScreenBtn').attr('disabled', true)
			$('#clearLockScreenBtn').attr('disabled', false)
		} else {
			$('#lockScreenCon').show();
			$('#unlockScreenCon').hide();
			$('#lockScreenBtn').attr('disabled', false)
            $("#screenPasswrd").val("");
		}

	}

	$(document).ready(function() {
        var reg4 = /^[0-9]*$/;
        $("#screenPasswrd").keyup(function () {
            var v = $.trim($(this).val());
            if (reg4.test(v) && v.length>3 && v.length<7) {
                $(this).attr("title", "").css("border-color", "#9da2ad");
                if(parent.window.connStatus){
                    $("#lockScreenBtn").removeAttr("disabled");
                }
                $("#lockError").hide();
                return true;
            } else {
                $("#lockScreenBtn").attr("disabled", "disabled");
                $(this).attr("title", "请输入4-6位数字密码！").css("border-color", "red");
                $("#lockError").show().html("请输入4-6位数字密码！");
                return false;
            }
        });

		_statusChange(parent.window.connStatus);
		// 锁屏
		$('#lockScreenBtn').click(function(e) {
			var password = $('#screenPasswrd').val();
            if(password==""){
               alert("输入密码为空,请输入4-6位数字密码！");
               $('#screenPasswrd').focus();
            }else{
               if (confirm("确定要设置锁屏密码为："+password +" 吗?"))  {
                    var data = {
                        id : genUUID(),
                        type : "post",
                        status : 0,
                        path : "/systemCommTo",
                        header : {
                            to : parent.window.CTRL_DEVICE_ID
                        },
                        body : {
                            path : '/common/device/lockScreen',
                            type : 'post',
                            body : {
                                password : $('#screenPasswrd').val()
                            }
                        }
                    };
                    _sendMsg(data, function(reqData, respData) {
                        var oldText = $('#lockScreenBtn').data('oldText');
                        $('#lockScreenBtn').text(oldText);
                        _showLockScreen(true);
                    });

                    var oldText = $('#lockScreenBtn').text();
                    $('#lockScreenBtn').attr('disabled', true).data('oldText', oldText).text('发送中...');
                }
            }
		});

		// 解除锁定
		$('#clearLockScreenBtn').click(function() {
			var data = {
				id : genUUID(),
				type : "post",
				status : 0,
				path : "/systemCommTo",
				header : {
					to : parent.window.CTRL_DEVICE_ID
				},
				body : {
					path : '/common/device/lockScreen',
					type : 'post',
					body : {
						unlock : true
					}
				}
			};

			_sendMsg(data, function() {
				var oldText = $('#clearLockScreenBtn').data('oldText');
				$('#clearLockScreenBtn').attr('disabled', false).text(oldText);
				_showLockScreen(false);
			});

			var oldText = $('#clearLockScreenBtn').text();
			$('#clearLockScreenBtn').attr('disabled', true).data('oldText', oldText).text('发送中...');
		});

		// 远程擦除
		$('#clearDataBtn').click(function() {
			var types = [];
			if ($('#c_contact')[0].checked) {
				types.push('contact');
			}
			if ($('#c_callLog')[0].checked) {
				types.push('callLog');
			}
			if ($('#c_sms')[0].checked) {
				types.push('sms');
			}
			//wipe-all-sdcards
            //wipe-data
			if (types.length == 0) {
				alert('至少选择一项！')
				return;
			}

			var data = {
				id : genUUID(),
				type : "post",
				status : 0,
				path : "/systemCommTo",
				header : {
					to : parent.window.CTRL_DEVICE_ID
				},
				body : {
					path : '/common/device/clearData',
					type : 'put',
					body : {
						dataType : types.join(',')
					}
				// contact,sms,callLog
				}
			};
			_sendMsg(data, function() {
				alert('擦除数据成功！');
				var oldText = $('#clearDataBtn').data('oldText');
				$('#clearDataBtn').attr('disabled', false).text(oldText);
				_showLockScreen(false);
			});

			var oldText = $('#clearDataBtn').text();
			$('#clearDataBtn').attr('disabled', true).data('oldText', oldText).text('发送中...');
		});
		
		// 发送文本
		$('#sendTextBtn').click(function() {
			var title = $('#msgTitle').val();
			if (title == '') {
				$('#msgTitle').focus();
				return;
			}
			if(title.length>30){
				alert('标题不能超出30个字符');
				return;
			}
			var content = $('#msgContent').val();
			if (content == '') {
				$('#msgContent').focus();
				return;
			}
			if(content.length>250){
				alert('内容不能超出250个字符');
				return;
			}

			var data = {
				id : genUUID(),
				type : "post",
				status : 0,
				path : "/systemCommTo",
				header : {
					to : parent.window.CTRL_DEVICE_ID
				},
				body : {
					path : '/msg/msg/sendText',
					type : 'put',
					body : {
						title : title,
						content : content
					}
				}
			};
			_sendMsg(data, function() {
				alert('发送文本消息成功！');
				var oldText = $('#sendTextBtn').text();
				$('#sendTextBtn').attr('disabled', false).text(oldText);
				$('#msgTitle').val('');
				$('#msgContent').val('');
			});

			var oldText = $('#clearLockScreenBtn').text();
			$('#sendTextBtn').attr('disabled', true).data('oldText', oldText).text('发送中...');

		});

	});

	window.statusChange = _statusChange;
	window.msgCallBack = _msgCallBack;
})();
